﻿using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.WinCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

namespace SmartPlant.WinCore
{
    /// <summary>
    /// 系统内存中保存的全局对象。
    /// </summary>
    public static class GlobalObject
    {
        private static string _authorizationCode = string.Empty;
        private static Project? _currentProject = null;
        private static PlantAreaUnit? _currentUnit = null;
        private static SysConfig? _sysConfig = null;
        //private static List<AttrDropDownListObj> _AllAttrDropDownList = new();
        private static Dictionary<string, List<DynamicPropertyInfo>> _propertyList = new();
        private static List<UserRightData> _userRightDatas = new List<UserRightData>();

        public static Dictionary<string, string> lookupDic = new()
        {
            {nameof(ComponentLocation),"L1084" },
            {nameof(ComponentMfr),"L1085" },
            {nameof(ComponentMod),"L1086" },
            {nameof(Line),"L1235" }
        };

        public static Dictionary<string, List<DynamicPropertyInfo>> PropertyList => _propertyList;
        /// <summary>
        /// EDE-api/index-default-columns的结果
        /// </summary>
        public static List<ViewQueryItemAttr> EDE_Index_AllColumns = new List<ViewQueryItemAttr>();
        /// <summary>
        /// 动态类的className
        /// </summary>
        public const string IndexTableEntityName = "IndexTableEntity";
        public const string HookUpComponentEntityName = "HookUpComponentEntity";

        static GlobalObject()
        {
            SysInformation = new SysInformation();
            SysInformation.OSArchitecture = RuntimeInformation.OSArchitecture.ToString();
            SysInformation.OSDescription = RuntimeInformation.OSDescription;
            SysInformation.NetVersion = RuntimeInformation.FrameworkDescription;
            Assembly assembly = typeof(SysInformation).Assembly;
            SysInformation.AssemblyVersion = assembly.GetName().Version.ToString();
        }

        /// <summary>
        /// 获取用户全局权限参数列表。
        /// </summary>
        public static List<UserRightData>? UserRightDatas { get => _userRightDatas; set => _userRightDatas = value; }

        /// <summary>
        /// 当前登陆的用户是否为管理员。
        /// </summary>
        public static string isSysAdminUser { get; set; }

        /// <summary>
        /// 当前登陆的用户。
        /// </summary>
        public static string currentUser { get; set; }

        /// <summary>
        /// 当前登陆的用户ID。
        /// </summary>
        public static string currentUserID { get; set; }



        public static List<Plant> Plants { get; set; }
        /// <summary>
        /// 当前登陆用户的授权token。
        /// </summary>
        public static string AuthorizationCode
        {
            get
            {
                return _authorizationCode;
            }
            set
            {
                _authorizationCode = value;
            }
        }

        /// <summary>
        /// 获取左边树形控件当前选择的节点。
        /// </summary>
        public static List<ITreeNodeModel> CurrentNodes { get; set; }
        /// <summary>
        /// 当前位号编号。
        /// </summary>
        public static string CurrentCmpntID { get; set; }
        /// <summary>
        /// 当前登陆用户打开的项目信息。
        /// </summary>
        public static Project CurrentProject
        {
            get
            {
                return _currentProject;
            }
            set
            {
                _currentProject = value;
            }
        }

        /// <summary>
        /// 当前登陆用户打开的项目信息。
        /// </summary>
        public static PlantAreaUnit CurrentUnit
        {
            get
            {
                return _currentUnit;
            }
            set
            {
                _currentUnit = value;
            }
        }
        /// <summary>
        /// 系统当前配置信息。
        /// </summary>
        public static SysConfig? CurrentConfig
        {
            get
            {
                return _sysConfig;
            }
            set
            {
                _sysConfig = value;
            }
        }

        #region 动态列全局对象。
        /// <summary>
        /// HookUpItem的动态属性信息，在系统登陆后就需要初始化。
        /// </summary>
        public static List<DynamicPropertyInfo> HookUpComponentPropertyInfos
        {
            get
            {
                if (_propertyList.ContainsKey(nameof(HookUpComponentPropertyInfos)))
                    return _propertyList[nameof(HookUpComponentPropertyInfos)];
                return null;
            }
            set
            {
                if (_propertyList.ContainsKey(nameof(HookUpComponentPropertyInfos)))
                    _propertyList[nameof(HookUpComponentPropertyInfos)] = value;
                else
                {
                    _propertyList.Add(nameof(HookUpComponentPropertyInfos), value);
                }
            }
        }
        /// <summary>
        /// HookUpItem的动态属性信息，在系统登陆后就需要初始化。
        /// </summary>
        public static List<DynamicPropertyInfo> HookUpItemPropertyInfos
        {
            get
            {
                if (_propertyList.ContainsKey(nameof(HookUpItemPropertyInfos)))
                    return _propertyList[nameof(HookUpItemPropertyInfos)];
                return null;
            }
            set
            {
                if (_propertyList.ContainsKey(nameof(HookUpItemPropertyInfos)))
                    _propertyList[nameof(HookUpItemPropertyInfos)] = value;
                else
                {
                    _propertyList.Add(nameof(HookUpItemPropertyInfos), value);
                }
            }
        }

        /// <summary>
        /// 索引表的动态属性信息，在系统登陆后就需要初始化。<see cref="SysServiceInit.InitAsync"/>。从<see cref="ViewQueryItemAttr"/>转过来的
        /// </summary>
        public static List<DynamicPropertyInfo> IndexTablePropertyInfos
        {
            get
            {
                if (_propertyList.ContainsKey(nameof(IndexTablePropertyInfos)))
                    return _propertyList[nameof(IndexTablePropertyInfos)];
                return null;
            }
            set
            {
                if (_propertyList.ContainsKey(nameof(IndexTablePropertyInfos)))
                    _propertyList[nameof(IndexTablePropertyInfos)] = value;
                else
                {
                    _propertyList.Add(nameof(IndexTablePropertyInfos), value);
                }
            }
        }

        /// <summary>
        /// 文档表的动态属性信息，在系统登陆后就需要初始化。
        /// </summary>
        public static List<DynamicPropertyInfo> DrawingDocPropertyInfos
        {
            get
            {
                if (_propertyList.ContainsKey(nameof(DrawingDocPropertyInfos)))
                    return _propertyList[nameof(DrawingDocPropertyInfos)];
                return null;
            }
            set
            {
                if (_propertyList.ContainsKey(nameof(DrawingDocPropertyInfos)))
                    _propertyList[nameof(DrawingDocPropertyInfos)] = value;
                else
                {
                    _propertyList.Add(nameof(DrawingDocPropertyInfos), value);
                }
            }
        }
        #endregion

        ///// <summary>
        ///// 动态列下拉列表选项，以proid_propId为Key保存数据。
        ///// </summary>
        //[Obsolete("要换成和属性id无关的key去存储，如E2032,L1542，因为可能多个属性都是相同的一套下拉，导致可能会有问题。")]
        //public static List<AttrDropDownListObj> AllPropertyDropDownList
        //{
        //    get { return _AllAttrDropDownList; }
        //}

        public static List<Enumeration> EnumerationsCollection = new List<Enumeration>();
        /// <summary>
        /// domain-conn时，从服务器中查询一次
        /// </summary>
        public static List<Lookuptables> LookuptablesCollection = new List<Lookuptables>();
        /// <summary>
        /// domain-conn时，从服务器中查询一次
        /// </summary>
        public static List<Itemattributions> ItemattributionsCollection = new List<Itemattributions>();

        /// <summary>
        /// domain-conn时，从服务器中查询一次
        /// </summary>
        public static List<Item> ItemsCollection = new List<Item>();

        public static List<UnitOfMeasure> UnitOfMeasureCollection = new List<UnitOfMeasure>();




        ///// <summary>
        ///// 根据属性ItemAttributionID获取动态列下拉列表选项。
        ///// </summary>
        ///// <param name="itemId"></param>
        ///// <returns></returns>
        //public static List<DropDownListObj>? XXGetDropDownListByAttr(decimal itemId)
        //{
        //    var itemAttr = ItemattributionsCollection.FirstOrDefault(x => x.Id == itemId);

        //    if (itemAttr == null) return null;

        //    var type = itemAttr.Attribution.Attribute.AttributeDatatype;


        //    if (type == "L1542")
        //    {
        //        var uomId = itemAttr.Attribution.Attribute.AttributeUomid;
        //        var res = new List<DropDownListObj>();
        //        if (uomId != 1007)
        //        {
        //            var col = GlobalObject.UnitOfMeasureCollection.Where(x => x.UoM_TYPE_ID == uomId);

        //            foreach (var uom in col)
        //            {
        //                res.Add(new DropDownListObj() { Id = uom.UomId.ToString(), Name = uom.UomCode });
        //            }
        //        }
        //        else
        //        {
        //            //1007 代表所有uom都可以选
        //            foreach (var uom in GlobalObject.UnitOfMeasureCollection)
        //            {
        //                res.Add(new DropDownListObj() { Id = uom.UomId.ToString(), Name = uom.UomCode });
        //            }


        //        }
        //        return res;
        //    }
        //    else
        //    {
        //        return DropDownListsAll.FirstOrDefault(x => x.ATTRIBUTE_DATATYPE == type)?.DropDownList;
        //    }


        //}


        /// <summary>
        /// 获取系统信息。
        /// </summary>
        public static SysInformation SysInformation { get; private set; }


    }
}
